package com.xinghai.service.impl;


import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xinghai.domain.JkqActivityShare;
import com.xinghai.domain.JkqUserCopy;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.transaction.annotation.Transactional;
import com.xinghai.common.constant.GenConstants;
import com.xinghai.mapper.JkqUserMapper;
import com.xinghai.domain.JkqUser;
import com.xinghai.service.IJkqUserService;

/**
 * 用户管理Service业务层处理
 *
 * @author xinghai
 * @date 2025-08-02
 */
@Service
public class JkqUserServiceImpl extends MPJBaseServiceImpl<JkqUserMapper, JkqUser>
        implements IJkqUserService
{

    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    /**
     * 查询用户管理列表
     *
     * @param jkqUser 用户管理
     * @return 用户管理集合
     */
    @Override
    public List<JkqUser> list(JkqUser jkqUser) {
        MPJLambdaWrapper<JkqUser> wrapper = new MPJLambdaWrapper<JkqUser>()
                .setAlias("u1")
                .selectAll(JkqUser.class)
                .selectAs(JkqUserCopy::getNickname,JkqUser::getHealthUserName)
                .leftJoin(JkqUserCopy.class,"u2",JkqUserCopy::getId,JkqUser::getHealthUserId);
        if (jkqUser.getNickname()!= null  && !jkqUser.getNickname().isEmpty()){
            wrapper.like("u1.nickname", jkqUser.getNickname());
        }
        if (jkqUser.getHealthUserId()!= null){
            wrapper.eq("u1.health_user_id", jkqUser.getHealthUserId());
        }
        if (jkqUser.getName()!= null  && !jkqUser.getName().isEmpty()){
            wrapper.like("u1.name", jkqUser.getName());
        }
        if (jkqUser.getPhone()!= null  && !jkqUser.getPhone().isEmpty()){
            wrapper.like("u1.phone", jkqUser.getPhone());
        }
        if (jkqUser.getStatus()!= null  && !jkqUser.getStatus().isEmpty()){
            wrapper.eq("u1.status", jkqUser.getStatus());
        }
        if (jkqUser.getType()!= null  && !jkqUser.getType().isEmpty()){
            wrapper.eq("u1.type", jkqUser.getType());
        }
        if (jkqUser.getParams().get("beginCreateTime")!= null && !"".equals(jkqUser.getParams().get("beginCreateTime"))
                &&jkqUser.getParams().get("endCreateTime")!= null && !"".equals(jkqUser.getParams().get("endCreateTime"))){
            wrapper.between("s.create_time", jkqUser.getParams().get("beginCreateTime"),jkqUser.getParams().get("endCreateTime"));
        }
        if (jkqUser.getParams().get("beginLastLoginTime")!= null && !"".equals(jkqUser.getParams().get("beginLastLoginTime"))
                &&jkqUser.getParams().get("endLastLoginTime")!= null && !"".equals(jkqUser.getParams().get("endLastLoginTime"))){
            wrapper.between("s.last_login_time", jkqUser.getParams().get("beginLastLoginTime"),jkqUser.getParams().get("endLastLoginTime"));
        }
        return this.baseMapper.selectJoinList(JkqUser.class,wrapper);
    }

    /**
     * 导入用户管理信息
     *
     * @param list 用户管理信息集合
     * @return 结果
     */
    @Override
    @Transactional
    public boolean importData(List<JkqUser> list){
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        try {
            JkqUserMapper jkqUserMapper = sqlSession.getMapper(JkqUserMapper.class);
            list.forEach(jkqUserMapper::insert);
            // 提交数据
            sqlSession.commit();
            sqlSession.rollback();
        } catch (Exception e) {
            sqlSession.rollback();
            return false;
        } finally {
            sqlSession.close();
        }
        return true;
    }

    @Override
    public JkqUser getInfoByOpenId(String openid) {
        return this.getOne(new QueryWrapper<JkqUser>().eq("open_id", openid));
    }
}
